package main

import (
	"bufio"
	"fmt"
	"io/ioutil"
	"os"
)

func writeDemo() {
	fileObj, err := os.OpenFile("./xx.txt", os.O_APPEND|os.O_CREATE|os.O_TRUNC, 0644)
	if err != nil {
		fmt.Printf("open file failed,err:%v\n", err)
		return
	}
	//write
	fileObj.Write([]byte("ZL hz  l!\n"))
	//writestring
	fileObj.Write([]byte("中国人!\n"))
	fileObj.Close()
}

func writeBybufio() {
	fileObj, err := os.OpenFile("./xx.txt", os.O_APPEND|os.O_CREATE|os.O_TRUNC, 0644)
	if err != nil {
		fmt.Printf("open file failed,err:%v\n", err)
		return
	}
	defer fileObj.Close()
	//创建一个写的对象
	wr := bufio.NewWriter(fileObj)
	wr.WriteString("赵金鼎") //写到缓存中
	wr.WriteString("赵金鼎123")
	wr.Flush() //将缓存中的内容写入文件
}

func writeByioutil() {
	str := "永丰屯12345"

	/* 	ret, err := ioutil.ReadFile("./main.go")
	   	if err != nil {
	   		fmt.Printf("read file failed,err：%v\n", err)
	   	}
	   	str1 := string(ret)
	   	err1 := ioutil.WriteFile("./xx.txt", []byte(str1), 0666)
	*/
	err := ioutil.WriteFile("./xx.txt", []byte(str), 0666)

	if err != nil {
		fmt.Printf("open file failed,err:%v\n", err)
		return
	}
}

//打开文件，写内容
func main() {
	//writeDemo()
	//writeBybufio()
	writeByioutil()
}
